// pages/myorder/myorderlist/myorderlist.js
import {
  getaftersalelist,
  searchAfterSale
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    datalist: [],
    pager: null,
    page: 1,
    isBottom: false,
    checkInfo: {
      id: "",
      name: "全部",
      avatar: "../../../assets/images/logo.png"
    }
  },
  onShow: function() {
    let self = this;
    let {
      active,
      checkInfo,
      page
    } = self.data;
    getaftersalelist((active + 1), checkInfo.id, page, (r) => {
      if (r.code == 0) {
        if (r.pager.last_page == 1) {
          self.setData({
            datalist: r.data,
            pager: r.pager,
            isBottom: true,
          })

        } else {
          self.setData({
            datalist: r.data,
            pager: r.pager,
            isBottom: false,
          })
        }

      }

    })
  },
  onChange(e) {
    let self = this;
    this.setData({
      active: e.detail.index
    })

    let {
      checkInfo,
      page
    } = self.data;
    getaftersalelist((e.detail.index + 1), checkInfo.id, page, (r) => {
      if (r.code == 0) {

        if (r.pager.last_page == 1) {
          self.setData({
            datalist: r.data,
            pager: r.pager,
            page: 1,
            isBottom: true,
          })

        } else {
          self.setData({
            datalist: r.data,
            pager: r.pager,
            page: 1,
            isBottom: false,
          })
        }

      }
    })
  },
  goAFCreate: function() {
    wx.navigateTo({
      url: '../aftersalecreate/aftersalecreate',
    })
  },
  //查看售后详情
  goAFDetail: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../aftersaledetailview/aftersaledetailview?id=' + id,
    })
  },
  //扫一扫获取二维码
  onScanCodeSearch: function() {
    wx.scanCode({
      success: (res) => {

        searchAfterSale(res.result, (r) => {
          if (r.code == 0) {
            if (r.data[0]) {
              wx.navigateTo({
                url: '../aftersaledetailview/aftersaledetailview?id=' + r.data[0].id,
              })
            } else {
              errorMsg(r.message)
            }
          }
        })
      },
      fail: (res) => {
        errorMsg("识别失败")
      }
    })
  },
  //跳转至搜索
  goSearch: function() {
    wx.navigateTo({
      url: '../aftersalelistsearch/aftersalelistsearch',
    })
  },
  //跳转到物流查询
  expressSearch: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../viewexpress/viewexpress?id=' + id,
    })
  },
  //跳转至售后人员列表选择
  goafusercheck: function() {
    let {
      active
    } = this.data;
    if (this.data.datalist[0]) {
      wx.navigateTo({
        url: '../afusercheck/afusercheck?tabid=' + (active + 1),
      })
    } else {
      errorMsg("暂无数据")
    }

  },
  preImg: function(e) {
    let src = e.currentTarget.dataset.src
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: [src] // 需要预览的图片http链接列表
    })
  },
  //下啦刷新
  onPullDownRefresh: function() {
    let self = this;
    let {
      active,
      checkInfo
    } = this.data;
    let page = 1;
    getaftersalelist((active + 1), checkInfo.id, page, (r) => {
      if (r.code == 0) {

        if (r.pager.last_page == 1) {
          self.setData({
            datalist: r.data,
            pager: r.pager,
            page: 1,
            isBottom: true,
          })

        } else {
          self.setData({
            datalist: r.data,
            pager: r.pager,
            page: 1,
            isBottom: false,
          })
        }
        wx.stopPullDownRefresh()
      }
    })
  },
  //上拉加载
  onReachBottom: function() {
    var self = this;
    let {
      active,
      checkInfo,
      page,
      pager,
    } = this.data;

    if (pager.last_page <= page) {
      self.setData({
        isBottom: true
      })
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page = page + 1;
      getaftersalelist((active + 1), checkInfo.id, page, (r) => {
        if (r.code == 0) {
          self.setData({
            datalist: self.data.datalist.concat(r.data),
            pager: r.pager,
            page: page
          })
        }
      })
    }
  },
})